home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: modifying a 'const' variable, a bug in VC++?
- Date: Tue, 27 Feb 1996 15:20:53 GMT
- Organization: Netcom
- Message-ID: <31332024.55676288@nntp.ix.netcom.com>
- References: <00001a81+0000a7f1@msn.com> <4gqm28$ci3@news.bridge.net>
- NNTP-Posting-Host: ix-dc12-08.ix.netcom.com
- X-NETCOM-Date: Tue Feb 27 7:20:42 AM PST 1996
- X-Newsreader: Forte Agent .99d/32.182
-
- David Byrden <100101.2547@compuserve.com> wrote:
-
- >
- >
- > >>>>>>>
- > const int i = 1;
- > int* j = (int *)&i; // cast a "const int*" to "int*". The
- > // compiler should allow it
- >
- > *j = 2;
- > cout << *j << newl; // should print "2"
- > cout << i << newl; // should print "2" too, prints 1
- >
- >
- > I consider this a bug in VC++(The correct result should be 2\n2\n.
- > Any comments?
-
- Why do you consider it a bug? The draft is quite clear (7.1.5.1):
-
- Except that any class member declared mutable can be modified,
- any attempt to modify a const object during its lifetime
- results in undefined behavior.
-
- This permits the compiler to do anything -- it's hard to have a bug
- when there are no requirements.
-
- i is defined as const and the compiler has the right to assume that
- code will not modify it. It optimized the use of i to simply use the
- constant.
-
-
- Michael M Rubenstein
-